<template>
  <div>
    <el-dialog
        :visible.sync="showDialog"
        :title="title"
        width="50%"
        center
        top="50px"
    >
      <el-row>
        <el-col :span="12">
          <div>
            <span style="width: 85px;display: inline-block;">预结算价格：</span>
            <el-input
                placeholder="请输入价格"
                size="small"
                style="width: 250px"
                v-model="form.preSettlementMoney"
                @input="handleMoneyInput"
            ></el-input>
          </div>
          <div style="margin-top: 15px;">
            <span style="width: 85px;display: inline-block;">下单日期：</span>
            <el-date-picker
                v-model="form.placeTime"
                type="date"
                style="width: 250px"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                size="small"
                placeholder="选择日期"
            >
            </el-date-picker>
          </div>
          <div style="margin-top: 15px;">
            <span style="width: 85px;display: inline-block;">到货日期：</span>
            <el-date-picker
                v-model="form.deliveryTime"
                type="date"
                style="width: 250px"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                size="small"
                placeholder="选择日期"
            >
            </el-date-picker>
          </div>
          <div style="margin-top: 15px">
            <span style="width: 85px;display: inline-block;">供货商类型:</span>
            <el-select size="small" clearable v-model="form.type"
                       placeholder="请公司类型" style="width: 250px">
              <el-option v-for="item in companyList" :key="item.type" :label="item.name"
                         :value="item.type">
              </el-option>
            </el-select>
          </div>
        </el-col>
        <el-col :span="12">
          <div>
            <span>设计图：</span>
            <upload-excel
                v-if="showDialog"
                style="margin-top: 10px"
                :type="'设计图'"
                @addStatus="handleUploadSuccess"
            />
          </div>

        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <div style="margin-top: 10px;">
            <span>上传合同：</span>
            <upload-excel
                v-if="showDialog"
                style="margin-top: 10px"
                :type="'合同'"
                @addStatus="handleUploadSuccess"
            />
          </div>
        </el-col>
        <el-col :span="12">
          <div style="margin-top: 15px">
            <span>预算单：</span>
            <upload-excel
                v-if="showDialog"
                style="margin-top: 10px"
                :type="'预算单'"
                @addStatus="handleUploadSuccess"
            />
          </div>
        </el-col>
      </el-row>
      <!-- <div style="height: calc(100vh - 300px); overflow-y: auto"></div> -->
      <span slot="footer">
        <el-button
            size="small"
            style="color: #666; background-color: #eee"
            @click="showDialog = false"
        >取 消</el-button
        >
        <el-button size="small" type="primary" @click="_submit"
        >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import uploadExcel from "./uploadExcel.vue";

export default {
  name: "preSettlement",
  components: {
    uploadExcel,
  },
  data() {
    return {
      showDialog: false,
      noDate: false,
      title: "供应商预结算上传",
      settlement: 1,
      form: {
        contractUrl: "",
        assignTaskId: "",
        preSettlementUrl: "",
        preSettlementMoney: "",
        placeTime: "",
        deliveryTime: "",
        designUrl: "",
        type: null
      },
      payProofList: [],
      designProofList: [],
      companyList: []
    };
  },
  computed: {
    headers() {
      const token = this.$quinn.getStorage("token");
      return {
        token,
      };
    },
  },
  created() {
    // this.getSupplierTypeInfo()
  },
  methods: {
    handleMoneyInput(val) {
      // 去掉非数字和小数点，禁止负号
      const cleaned = val.replace(/[^\d.]/g, '')
          .replace(/^0+(\d)/, '$1')  // 禁止前缀多个 0
          .replace(/\.{2,}/g, '.')   // 连续小数点 -> 1 个
          .replace(/^(\d+\.\d{2}).*$/, '$1'); // 保留两位小数

      this.form.preSettlementMoney = cleaned;
    },
    getSupplierTypeInfo() {
      this.$api.companyGetSupplierTypeInfo().then(res => {
        this.companyList = res
      })
    },
    handleOpen(row, title) {
      this.showDialog = true;
      this.getSupplierTypeInfo()
      this.form = this.$options.data().form;
      this.form.assignTaskId = row.assignTaskId;
    },
    paySuc(res) {
      if (res.code == 200 && res.result) {
        if (this.payProofList) {
          this.payProofList.push(res.result[0]);
        } else {
          this.payProofList = [res.result[0]];
        }
      }
    },
    paySuc2(res) {
      if (res.code == 200 && res.result) {
        if (this.designProofList) {
          this.designProofList.push(res.result[0]);
        } else {
          this.designProofList = [res.result[0]];
        }
      }
    },
    deleteDesign(i) {
      this.payProofList.splice(i, 1);
    },
    deletePic(i) {
      this.designProofList.splice(i, 1);
    },
    handleUploadSuccess(val) {
      if (val.type == "预算单") {
        delete val.type;
        this.form.preSettlementUrl = val.file;
      } else if (val.type == "设计图") {
        delete val.type;
        this.form.designUrl = val.file;
      } else if (val.type == "合同") {
        delete val.type;
        this.form.contractUrl = val.file;
      }
    },
    _submit() {
      this.$api.budgetAddSupplierTaskBudget(this.form).then((res) => {
        if (res == 1) {
          this.$message.success("提交成功！");
          this.showDialog = false;
          this.$emit("addStatus", 1);
        }
      });
    },
  },
};
</script>

<style scoped>
span {
  font-weight: bold;
}
</style>
